package com.thirdsdk.zhugeio;

import android.content.Context;

import com.curry.android.util.SystemInfoUtil;
import com.zhuge.analysis.stat.ZhugeSDK;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 诸葛IO帮助类
 * Created by 绯若虚无 on 2016/4/27.
 *
 * @author wgf
 * @since v3.6.2
 */
public class ZhugeIOHelper {

    /** 添加到诸葛IO */
    public static void setIdentify(Context context, String loginID, String name, String mobile) {
        JSONObject jo = new JSONObject();
        try {
            jo.put("name", name);
            jo.put("mobile", mobile);
            ZhugeSDK.getInstance().identify(context, loginID, jo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //"EnterFrontPage"事例
    public static void trackEnterFrontPage(Context context, String commName, String city) {
        try {
            JSONObject eventObject = addCommonInfo(context);
            eventObject.put("commID", commName);
            eventObject.put("city", city);
            ZhugeSDK.getInstance().track(context, "EnterFrontPage", eventObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private static JSONObject addCommonInfo(Context context) throws JSONException {
        JSONObject eventObject = new JSONObject();
        eventObject.put("deviceID", SystemInfoUtil.getDeviceID(context));
        eventObject.put("appVersion", SystemInfoUtil.getVersionName(context));
        eventObject.put("ts", String.valueOf(System.currentTimeMillis()));
        eventObject.put("mobileOS", "android");
        eventObject.put("mobileVersion", android.os.Build.VERSION.RELEASE);
        eventObject.put("mobileModel", android.os.Build.MODEL);
        return eventObject;
    }

    //"SelectLocation"事例
    public static void trackSelectLocation(Context context, boolean isAutoLocate) {
        try {
            JSONObject eventObject = addCommonInfo(context);
            /*手动定位或自动定位*/
            if (isAutoLocate) {
                eventObject.put("locateType", "自动定位");
            } else {
                eventObject.put("locateType", "手动定位");
            }
            ZhugeSDK.getInstance().track(context, "SelectLocation", eventObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //"ClickLoginButton"事例
    public static void trackClickLoginButton(Context context) {
        try {
            JSONObject eventObject = addCommonInfo(context);
            ZhugeSDK.getInstance().track(context, "ClickLoginButton", eventObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //"GetVerificationCode"事例
    public static void trackGetVerificationCode(Context context, int reclick, boolean isSmsVerify) {
        try {
            JSONObject eventObject = addCommonInfo(context);
            eventObject.put("reClick", reclick);
            if (isSmsVerify) {
                eventObject.put("verifyType", "短信验证");
            } else {
                eventObject.put("verifyType", "语音验证");
            }
            ZhugeSDK.getInstance().track(context, "GetVerificationCode", eventObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //"EnterRegisterPage"事例
    public static void trackRegisterPage(Context context) {
        try {
            JSONObject eventObject = addCommonInfo(context);
            ZhugeSDK.getInstance().track(context, "EnterRegisterPage", eventObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //"EnterGuidePage"事例
    public static void trackEnterGuidePage(Context context) {
        try {
            JSONObject eventObject = addCommonInfo(context);
            ZhugeSDK.getInstance().track(context, "EnterGuidePage", eventObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //"EnterApp"事例
    public static void trackEnterApp(Context context) {
        try {
            JSONObject eventObject = addCommonInfo(context);
            ZhugeSDK.getInstance().track(context, "EnterApp", eventObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
